package com.zjy.controller;

import com.zjy.bean.Order;
import com.zjy.dto.*;
import com.zjy.service.OrderService;
import com.zjy.utils.ObjectUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;

import static com.zjy.api.Api.CLIENT_ERROR;
import static com.zjy.api.Api.SERVER_ERROR;

/**
 * Created by Administrator on 2019/5/20.
 */
@Controller
@CrossOrigin
@RequestMapping(value = "/order")
public class OrderController {
    @Resource
    private OrderService orderService;

    /**
     * 生成订单
     * @param orderDTO
     * @return
     */
    @RequestMapping(value = "/addOrder",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult addOrder(@RequestBody OrderDTO orderDTO){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(orderDTO.getOrder()) || ObjectUtils.isNullOrEmpty(orderDTO.getShoppingCartIdList())){
            return result.error(CLIENT_ERROR,"订单信息和购物车Id数组不能为空");
        }else {
            try{
                String orderId = orderService.addOrder(orderDTO);
                result.setMsg("下单成功！");
                result.setData(orderId);
            }catch (Exception e){
                return result.error(SERVER_ERROR,"下单失败，原因："+ e.getMessage());
            }
        }
        return result;
    }

    /**
     * 订单付款
     * @param order
     * @return
     */
    @RequestMapping(value = "/payOrder",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult payOrder(@RequestBody Order order){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(order.getId())){
            return result.error(CLIENT_ERROR,"订单id不能为空");
        }else {
            try{
                int count = orderService.payOrder(order.getId());
                if(count == 1){
                    result.setMsg("订单付款成功！");
                }else if(count == 0){
                    result.error(CLIENT_ERROR,"找不到此id对应的订单");
                }
            }catch (Exception e){
                return result.error(SERVER_ERROR,"订单付款失败，原因："+ e.getMessage());
            }
        }
        return result;
    }

    /**
     * 删除订单
     * @param listDTO
     * @return
     */
    @RequestMapping(value = "/deleteOrderById",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult deleteOrderById(@RequestBody ListDTO listDTO){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(listDTO.getIdList())){
            return result.error(CLIENT_ERROR,"订单id不能为空");
        }else {
            try{
                int count = orderService.deleteOrderById(listDTO.getIdList());
                if(count == 1){
                    result.setMsg("删除订单成功！");
                }else if(count == 0){
                    result.error(CLIENT_ERROR,"找不到此id对应的订单");
                }
            }catch (Exception e){
                return result.error(SERVER_ERROR,"下单失败，原因："+ e.getMessage());
            }
        }
        return result;
    }

    /**
     * 订单发货
     * @param order
     * @return
     */
    @RequestMapping(value = "/deliverOrder",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult deliverOrder(@RequestBody Order order){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(order.getId())){
            return result.error(CLIENT_ERROR,"订单id不能为空");
        }else {
            try{
                int count = orderService.deliverOrder(order.getId());
                if(count == 1){
                    result.setMsg("订单发货成功！");
                }else if(count == 0){
                    result.error(CLIENT_ERROR,"找不到此id对应的订单");
                }
            }catch (Exception e){
                return result.error(SERVER_ERROR,"订单发货失败，原因："+ e.getMessage());
            }
        }
        return result;
    }

    /**
     * 完成订单
     * @param order
     * @return
     */
    @RequestMapping(value = "/completeOrder",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult completeOrder(@RequestBody Order order){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(order.getId())){
            return result.error(CLIENT_ERROR,"订单id不能为空");
        }else {
            try{
                int count = orderService.completeOrder(order.getId());
                if(count == 1){
                    result.setMsg("订单已完成！");
                }else if(count == 0){
                    result.error(CLIENT_ERROR,"找不到此id对应的订单");
                }
            }catch (Exception e){
                return result.error(SERVER_ERROR,"订单完成失败，原因："+ e.getMessage());
            }
        }
        return result;
    }

    /**
     * 根据订单id查询订单详情
     * @param order
     * @return
     */
    @RequestMapping(value = "/selectOrderById",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult selectOrderById(@RequestBody Order order){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(order.getId())){
            return result.error(CLIENT_ERROR,"订单id不能为空");
        }else {
            try{
                OrderQTO orderQTO = orderService.selectOrderById(order.getId());
                if(!ObjectUtils.isNullOrEmpty(orderQTO)){
                    result.setMsg("订单查询成功！");
                    result.setData(orderQTO);
                }else {
                    result.error(CLIENT_ERROR,"找不到此id对应的订单");
                }
            }catch (Exception e){
                return result.error(SERVER_ERROR,"订单查询失败，原因："+ e.getMessage());
            }
        }
        return result;
    }

    /**
     * 查询符合条件的订单列表
     * @param orderParam
     * @return
     */
    @RequestMapping(value = "/selectOrderList",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult selectOrderList(@RequestBody OrderParam orderParam){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(orderParam)){
            return result.error(CLIENT_ERROR,"参数不能为空");
        }else {
            try{
                //List<OrderQTO> orderQTOList = orderService.selectOrderList(orderParam);
                ListResponse response = orderService.selectOrderList(orderParam);
                if(!ObjectUtils.isNullOrEmpty(response)){
                    result.setMsg("订单查询成功！");
                    result.setData(response);
                }else {
                    result.setMsg("找不到此对应的订单");
                }
            }catch (Exception e){
                return result.error(SERVER_ERROR,"订单查询失败，原因："+ e.getMessage());
            }
        }
        return result;
    }

    /**
     * 查询今日订单总数、今日销售额、全部订单总数
     * @return
     */
    @RequestMapping(value = "/getOrderData",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getOrderData(){
        ApiResult result = new ApiResult();
        try{
            HashMap<String,Object> map = orderService.getOrderData();
            if(!ObjectUtils.isNullOrEmpty(map)){
                result.setMsg("数据查询成功！");
                result.setData(map);
            }else {
                result.setMsg("数据查询失败");
            }
        }catch (Exception e){
            return result.error(SERVER_ERROR,"数据查询失败，原因："+ e.getMessage());
        }
        return result;
    }

}
